/* --------------------------------------------------------
 * Copyright (c) aeky , Inc.  All rights reserved.
 * --------------------------------------------------------
 */
package com.aeky.client.view.main;

import com.aeky.client.ui.AekyPortalColumn;
import com.aeky.client.ui.AekyPortalLayout;
import com.aeky.client.ui.AekyPortlet;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.AnimationCallback;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.layout.LayoutSpacer;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * 
 * @author aeky
 */
public class MainPortal extends VLayout {

    public MainPortal() {
        this.setMargin(10);
        final AekyPortalLayout portalLayout = new AekyPortalLayout();
        this.setWidth100();
        this.setHeight100();

        // create portlets...
        for (int i = 1; i <= 5; i++) {
            AekyPortlet portlet = new AekyPortlet();
            portlet.setTitle("Portlet");

            Label label = new Label();
            label.setAlign(Alignment.CENTER);
            label.setLayoutAlign(VerticalAlignment.CENTER);
            label.setContents("Portlet contents");
            // label.setBackgroundColor(colors[Random.nextInt(colors.length -
            // 1)]);
            portlet.addItem(label);
            portalLayout.addPortlet(portlet);
        }

        final DynamicForm form = new DynamicForm();
        form.setAutoWidth();
        form.setNumCols(5);

        final StaticTextItem numColItem = new StaticTextItem();
        numColItem.setTitle("Columns");
        numColItem.setValue(portalLayout.getMembers().length);

        ButtonItem addColumn = new ButtonItem("addColumn", "Add Column");
        // addColumn.setIcon("silk/application_side_expand.png");
        addColumn.setAutoFit(true);
        addColumn.setStartRow(false);
        addColumn.setEndRow(false);

        addColumn.addClickHandler(new com.smartgwt.client.widgets.form.fields.events.ClickHandler() {
            public void onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent event) {
                // portalLayout.addMember(new AekyPortalColumn());
                numColItem.setValue(portalLayout.getMembers().length);

            }
        });

        ButtonItem removeColumn = new ButtonItem("removeColumn", "Remove Column");
        // removeColumn.setIcon("silk/application_side_contract.png");
        removeColumn.setAutoFit(true);
        removeColumn.setStartRow(false);
        removeColumn.setEndRow(false);

        removeColumn.addClickHandler(new com.smartgwt.client.widgets.form.fields.events.ClickHandler() {
            public void onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent event) {

                Canvas[] canvases = portalLayout.getMembers();
                int numMembers = canvases.length;
                if (numMembers > 0) {
                    Canvas lastMember = canvases[numMembers - 1];
                    portalLayout.removeMember(lastMember);
                    numColItem.setValue(numMembers - 1);
                }

            }
        });

        final ButtonItem addPortlet = new ButtonItem("addPortlet", "Add Portlet");
        // addPortlet.setIcon("silk/application_view_tile.png");
        addPortlet.setAutoFit(true);

        addPortlet.setStartRow(false);
        addPortlet.setEndRow(false);
        addPortlet.addClickHandler(new com.smartgwt.client.widgets.form.fields.events.ClickHandler() {
            public void onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent event) {

                final AekyPortlet newPortlet = new AekyPortlet();
                newPortlet.setTitle("Portlet ");

                Label label = new Label();
                label.setAlign(Alignment.CENTER);
                label.setLayoutAlign(VerticalAlignment.CENTER);
                label.setContents("Portlet contents");
                // label.setBackgroundColor(colors[Random.nextInt(colors.length
                // - 1)]);
                newPortlet.addItem(label);

                newPortlet.setVisible(false);
                AekyPortalColumn column = portalLayout.addPortlet(newPortlet);

                // also insert a blank spacer element, which will trigger the
                // built-in
                // animateMembers layout animation
                final LayoutSpacer placeHolder = new LayoutSpacer();
                placeHolder.setRect(newPortlet.getRect());
                column.addMember(placeHolder, 0); // add to top

                // create an outline around the clicked button
                final Canvas outline = new Canvas();
                outline.setLeft(form.getAbsoluteLeft() + addPortlet.getLeft());
                outline.setTop(form.getAbsoluteTop());
                outline.setWidth(addPortlet.getWidth());
                outline.setHeight(addPortlet.getHeight());
                outline.setBorder("2px solid #8289A6");
                outline.draw();
                outline.bringToFront();

                outline.animateRect(newPortlet.getPageLeft(), newPortlet.getPageTop(), newPortlet.getVisibleWidth(),
                        newPortlet.getViewportHeight(), new AnimationCallback() {
                            public void execute(boolean earlyFinish) {
                                // callback at end of animation - destroy
                                // placeholder and outline; show the new portlet
                                placeHolder.destroy();
                                outline.destroy();
                                newPortlet.show();
                            }
                        }, 750);
            }
        });

        form.setItems(numColItem, addPortlet, addColumn, removeColumn);

        this.addMember(form);
        this.addMember(portalLayout);
        // this.draw();
    }
}
